﻿using System.Data;
using ApplicationLibrary.DBManager;
using ApplicationLibrary.ExtendedControls;
using ApplicationLibrary.SqlConnector;
using Vintner.Items.ModuleData;
using Vintner.Masters.ModuleData;
using System.Collections.Generic;

namespace Vintner.Items.Scrounger
{
    internal class MasterScrounger : ApplicationLibrary.DBManager.Scrounger
    {
        private const string ItemCustomerSlave = "ITEMCUSTOMERSLAVE";

        private UserTabControl<TabItemCustomer> UtctabJE = null;
        private TabItemCustomer tabItemcustomer = null;
        private Masters.Forms.Customers frm;

        public MasterScrounger(iScrounger frm)
            : base(frm)
        {
            this.frm = (Masters.Forms.Customers)frm;
        }

        public override void InitializerComponent()
        {
            UtctabJE = new UserTabControl<TabItemCustomer>(Properties.Resources.TD_ItemCustomer);
            UtctabJE.Name = "tpgItemCustomer";

            frm.MasterTabControl.Controls.Add(UtctabJE);
            tabItemcustomer = UtctabJE.UserControl;

            base.InitializerComponent();
        }

        public override void OnAttachData(DBManager dbManager)
        {
            dbManager.AddSlave(VI_ItemCustomerPrice.Name, true);
            dbManager.AddRelation(ItemCustomerSlave, VI_Customers.Customer, VI_ItemCustomerPrice.Customer, true);
            tabItemcustomer.OnAttachData(dbManager);

            base.OnAttachData(dbManager);
        }

        public override void OnPrepareAuxData()
        {
            tabItemcustomer.OnPrepareAuxData();

            base.OnPrepareAuxData();
        }

        public override void OnBindData()
        {
            tabItemcustomer.OnBindData(frm);
            base.OnBindData();
        }

        public override string CreateSlaveQuery(string name)
        {
            QueryBuilder qb = new QueryBuilder();
            if (name == VI_ItemCustomerPrice.Name)
            {
                qb.AddSelect();
                qb.AddFrom(VI_ItemCustomerPrice.Name);
                qb.AddCompare(VI_ItemCustomerPrice.Customer, "@p2");
                qb.AddOrder(VI_ItemCustomerPrice.Category);
            }
            return qb.Query;
        }

        public override System.Collections.Generic.List<SqlABParameter> CreateSlaveParam(string name)
        {
            if (name == VI_ItemCustomerPrice.Name)
            {
                List<SqlABParameter> PList = new List<SqlABParameter>();

                SqlABParameter sParam = new SqlABParameter("@p2", VI_ItemCustomerPrice.Customer);
                sParam.Value = VI_ItemCustomerPrice.Customer.DefaultValue;
                PList.Add(sParam);
                return PList;
            }
            return null;
        }

        public override void SetParameters(DBManager dbManager, IRadarParameters key, DBCollection collection)
        {
            collection.Parameter[0].Value = key[0];
        }

        public override void RowUpdating(DBManager dbManager, SqlABRowUpdatingEventArgs e)
        {
            if (e.StatementType == StatementType.Insert)
            {
                e.Row[VI_ItemCustomerPrice.Customer.Name] = dbManager.GetColumn<string>(VI_Customers.Customer, dbManager.Pos);
            }
            base.RowUpdating(dbManager, e);
        }
    }
}